<template>
  <div id="bottom" class="echoice">
    <el-form class="echoiceborder" label-width="80px" :model="echoiceinfo" :rules="rules" ref="echoiceinfo">
      <el-form-item label="题目名称" prop="name">
        <el-input v-model="echoiceinfo.name" type="textarea" autosize></el-input>
      </el-form-item>
      <div class="upload_image">
        <imgupload @img="geturl"></imgupload>
        <img :src="echoiceinfo.imgUrl" v-if="echoiceinfo.imgUrl" width="300" height="250"/>
      </div>
      <div v-for="index in chooselist" key="index">
        <el-form-item :label='"选项"+String.fromCharCode(index+64)' class="echoicename" prop="echoice">
          <el-input v-model="echoiceinfo.echoice[index-1]" type="textarea" autosize></el-input>
        </el-form-item>
      </div>
      <el-form-item label="正确选项" prop="radio">
        <el-radio-group v-model="echoiceinfo.radio" v-for='index in chooselist' :key="index">
          <el-radio :label="index" style="margin-left: 20px;">{{'选项'+String.fromCharCode(index+64)}}</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <el-button class="topicsavebtn" type="primary" @click="saveechoice('echoiceinfo')">保存</el-button>
  </div>
</template>

<script>
  import imgupload from './ImgUpload'

  export default {
    name: 'Ehoiceedit',
    components: {
      imgupload
    },
    data() {
      return {
        choosetrue: '正确选项',
        chooselist: [1, 2, 3, 4],
        echoiceinfo: {
          name: '',
          questionType: 0,
          echoice: [],
          radio: [],
          imgUrl: ''
        },
        rules: {
          name: [{
            required: true,
            message: '请输入题目名称',
            trigger: 'blur'
          }],
          echoice: [{
            required: true,
            message: '请输入选项',
            trigger: 'blur'
          }],
          radio: [{
            required: true,
            message: '请选择正确答案',
            trigger: 'blur'
          }]
        }
        // edit: false,
        // description: "你好！"
      }
    },
    methods: {
      geturl(url) {
        this.echoiceinfo.imgUrl = url;
      },
      saveechoice(echoiceinfo) {
        this.$refs[echoiceinfo].validate((valid) => {
          if (valid) {
            this.$emit('func', this.echoiceinfo);
          } else {
            // console.log('error submit!!');
            this.$message('请输入完整');
            return false;
          }
        });
      }
    }
  }
</script>

<style>
  .echoice {
    display: flex;
    flex-direction: column;
    text-align: left;
    width: 80%;
    background-color: #f5f5f5;
  }

  .echoiceborder {
    margin: 0.3125rem;
  }

  .upload_image {
    display: flex;
    flex-direction: row-reverse;
  }

  .topicname {
    font-size: 18px;
    font-weight: bold;
    text-align: center;
    margin: 0.3125rem;
    display: flex;
  }

  .echoicename {
    /* display: flex; */
  }

  .echoices {
    width: 100px;
    text-align: center;
  }

  .iconfontsize {
    font-size: 20px;
  }

  .topicsavebtn {
    margin: 5px;
  }
</style>
